package com.skyon.rule.res.spring.grid;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.drools.util.ServiceRegistry;
import org.drools.util.ServiceRegistryImpl;

public class GridNodeImpl implements GridNode {

	private String id;
	private final Map<String, Object> localContext = new ConcurrentHashMap<String, Object>();
	private final ServiceRegistry     serviceRegistry = ServiceRegistryImpl.getInstance();
	 
	public GridNodeImpl() {
		this.id = UUID.randomUUID().toString();
	}

	public GridNodeImpl(String id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.skyon.rule.res.spring.grid.GridNode#get(java.lang.String, java.lang.Class)
	 */
	public <T> T get(String identifier, Class<T> cls) {
		T service = (T) localContext.get(identifier);
        if ( service == null ) {
            service = this.serviceRegistry.get( cls );
        }
		return service;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.skyon.rule.res.spring.grid.GridNode#get(java.lang.Class)
	 */
	public <T> T get(Class<T> serviceClass) {
		return get(serviceClass.getName(), serviceClass);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.skyon.rule.res.spring.grid.GridNode#set(java.lang.String, java.lang.Object)
	 */
	public void set(String identifier, Object object) {
		this.localContext.put(identifier, object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.skyon.rule.res.spring.grid.GridNode#getId()
	 */
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void dispose() {
	}

	public void init(Object context) {
	}

}
